package cn.zyjw.service.impl;

import cn.zyjw.domain.SysRolePrivilege;
import cn.zyjw.mapper.SysRolePrivilegeMapper;
import cn.zyjw.service.SysRolePrivilegeService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zyjw.mapper.SysPrivilegeMapper;
import cn.zyjw.domain.SysPrivilege;
import cn.zyjw.service.SysPrivilegeService;

@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege> implements SysPrivilegeService {

    @Autowired
    private SysRolePrivilegeMapper sysRolePrivilegeMapper;

    /**
     * 获取菜单下的权限列表
     *
     * @param menuId
     * @param roleId
     * @return
     */
    @Override
    public List<SysPrivilege> getPrivileges(Long menuId, Long roleId) {
        List<SysPrivilege> list = list(Wrappers.<SysPrivilege>lambdaQuery()
                .eq(SysPrivilege::getMenuId, menuId)
        );
        //查询角色的下的权限
        Set<Long> ids = sysRolePrivilegeMapper.selectPrivilegeIdsByRoleId(roleId);
        for (SysPrivilege sysPrivilege : list) {
            if (ids.contains(sysPrivilege.getId())) {
                sysPrivilege.setOwn(1);
            }
        }
        return list;
    }
}
